package com.lanou.controller;

import com.lanou.entity.UserSearchRecord;
import com.lanou.service.UserSearchRecordService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserSearchRecordController
 * @Description TODO
 * @Author cuipengwei
 * @Date 2025年08月14日 16:12
 * @Version 1.0
 */
@RestController
@RequestMapping("userSearchRecord")
public class UserSearchRecordController {
    @Autowired
    private UserSearchRecordService userSearchRecordService;
    //增加
    @PostMapping
    public R add(@RequestBody UserSearchRecord userSearchRecord) {
        Integer i = userSearchRecordService.addUserSearchRecord(userSearchRecord);
        return R.ok(i);
    }
    //删除
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") Integer id){
        Integer i = userSearchRecordService.deleteUserSearchRecord(id);
        return R.ok(i);
    }
    //查询全部
    @GetMapping("all")
   public R getAllUserSearchRecord(){
       List<UserSearchRecord> allUserSearchRecord = userSearchRecordService.getAllUserSearchRecord();
       return R.ok(allUserSearchRecord);
   }
   //根据id查询
    @GetMapping("{id}")
    public R getUserSearchRecordById(@PathVariable("id") Integer id){
        UserSearchRecord userSearchRecordById = userSearchRecordService.getUserSearchRecordById(id);
        return R.ok(userSearchRecordById);
    }
    //清楚全部
    @DeleteMapping("clear")
    public R clearByUserId(@RequestParam Integer userId) {
        if (userId == null || userId <= 0) {
            return R.error("用户ID无效");
        }
        Integer count = userSearchRecordService.deleteByUserId(userId);
        return R.ok("已删除 " + count + " 条记录");
    }
    //根据用户id查询
    @GetMapping("user/{userId}")
    public R getUserSearchRecordByUserId(@PathVariable("userId") Integer userId) {
        List<UserSearchRecord> allUserSearchRecordByUserId = userSearchRecordService.getAllUserSearchRecordByUserId(userId);
        return R.ok(allUserSearchRecordByUserId);
    }
}
